class ArticlesController < ApplicationController

  def index
    @articles = Article.for_index.page params[:page]
    drop_meta title: 'Blog'
  end

  def show
    @article = Article.published.where(slug: params[:article_slug]).limit(1).first
    raise ActiveRecord::RecordNotFound if @article.blank?
    @prev_article = Article.published.where('id < ?', @article.id).order('id DESC').limit(1)[0]
    @next_article = Article.published.where('id > ?', @article.id).order(:id).limit(1)[0]
    drop_title "Blog - #{@article.title}"
  end

  def category_index
    @category = Category.where(slug: params[:category_slug]).limit(1).first
    raise ActiveRecord::RecordNotFound if @category.blank?
    @articles = @category.articles.for_index.page params[:page]
    drop_title @category.name
    render :index
  end

  def archives
    @archives = Article.archives.group_by { |article| article.published_at.year }
    drop_title 'Archives'
  end
end
